<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list.slice((listQuery.page-1)*listQuery.limit,listQuery.page*listQuery.limit)"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column prop="id" align="center" label="ID" width="80">
        <!-- <template v-slot="scope">
          <span>{{ scope.row.id }}</span>
        </template> -->
      </el-table-column>

      <el-table-column align="center" label="分类名称" width="auto">
        <template v-slot="scope">
          <span>{{ scope.row.category_name }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      :current-page="listQuery.page"
      :page-sizes="[1,2,5,10]"
      :page-size="listQuery.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { fetchCategoryList } from '@/api/category'
// import Pagination from '@/components/Pagination'

export default {
  name: 'Category',
  // components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        query: '',
        // 当前的页码
        page: 1,
        // 当前每页显示多少条数据
        limit: 2
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchCategoryList().then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
        console.log(`每页 13 条`)
      })
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.listQuery.limit = val
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.listQuery.page = val
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
